/*
 * This file is part of Space Invaders: Rehash
 * Copyright (C)2007 Vicent Marti (tanoku@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
#ifndef	_MAIN_H_
#define	_MAIN_H_

#include "SDL.h"
#include "SDL_mixer.h"
#include "string.h"
#include "gfx.h"
#include "font_handler.h"
#include "timer.h"

// FIXME: there isn't a fucking PI in math.h?
#define PI 3.14159265

#define	SCREEN_W	640
#define	SCREEN_H	480

// TODO: Disable this for release
//#define DEBUG_JUMP_TO_GAME
//#define USE_SKIP_CHEAT
//#define _SPACEINVADERS_DEBUG

#define FRAMES_PER_SECOND 60
#define CAP_FPS

#define KLEFT 0
#define KRIGHT 1
#define KSHOOT 2
#define K0 3
#define K1 4
#define K2 5
#define K3 6
#define K4 7
#define K5 8
#define K6 9
#define K7 10
#define K8 11
#define K9 12
#define KQ 13
#define KW 14
#define KE 15
#define KR 16
#define KPAUSE 17
#define KUP 18
#define KDOWN 19
#define KSCAPE 20
#define KCHEAT 21
#define KEY_MAX 22

#define SCREEN_NONE 0
#define SCREEN_LOAD 1
#define SCREEN_MAINMENU 2
#define SCREEN_CUTSCENE 3
#define SCREEN_GAME_TEST 4
#define SCREEN_CUTSCENE_TEST 5
#define SCREEN_GAME_CUSTOM 6
#define SCREEN_GAME_NEW 7
#define SCREEN_GAME_NEWLEVEL 8
#define SCREEN_GAME_NEWCUTSCENE 9
#define SCREEN_GAME_CONTINUE 10

#define ANIMATION_NONE 0
#define ANIMATION_CHANGE 1
#define ANIMATION_FADEIN 2
#define ANIMATION_SCROLLIN 3
#define ANIMATION_SCROLLOUT 4
#define ANIMATION_FADEOUT 5

#define SOUND_SHOOT 0
#define SOUND_ALIENDEAD 1
#define SOUND_PLAYERDEAD 2
#define SOUND_INVADER1 3
#define SOUND_MISSILE 4
#define SOUND_MENU1 5
#define SOUND_MENU2 6
#define SOUND_MUSIC 7
#define SOUND_MAX 8

extern SDL_Surface	*g_Screen;
extern FontHandler *g_Fonts;
extern bool g_Keys[KEY_MAX];
extern float g_ScreenRatio;
extern GLuint g_Textures[TEXTURE_COUNT];
extern GLuint g_Models[MODEL_COUNT];
extern Mix_Chunk *g_Sounds[SOUND_MAX];

class SI_Screen
{
public:
	virtual ~SI_Screen() {}
	virtual void tick() = 0;
	virtual void onEvent( SDL_Event *event ) = 0;
	virtual bool finished() = 0;
	virtual int getNextScreen() = 0;
};

class Intro : public SI_Screen
{
	bool m_finished;

	int m_animation;
	SimpleTimer m_animationTimer;

public:
	Intro()
	{
		m_finished = false;
		m_animation = ANIMATION_NONE;
		m_animationTimer.start();
	}
	
	void render();
	bool finished() { return m_finished; }
	void tick()
	{
		if ( m_animation == ANIMATION_NONE && m_animationTimer.get_ticks() > 3000 )
		{
			m_animation = ANIMATION_FADEOUT;
			m_animationTimer.start();
		}
		render();
	}
	void onEvent( SDL_Event *event ) {}
	int getNextScreen()
	{
		return SCREEN_MAINMENU;
	}
};

class GameManager
{
public:
	GameManager();
	
	void tick();
	void onEvent( SDL_Event *event );
	
	void newScreen( int nextScreen );

private:
	SI_Screen *m_activeScreen;
	int m_nextScreen;
	int m_seenRules;
	int m_currentLevel;
	
};

#endif
